package com.mexico.inloancash.india_modelimpl;


import com.google.gson.Gson;
import com.mexico.inloancash.MyApplication;
import com.mexico.inloancash.bean.India_User_Login_Bean;
import com.mexico.inloancash.callback.IndiaCallBack;
import com.mexico.inloancash.contract.IndiaLoginContract;
import com.mexico.inloancash.http.IndiaRequestApi;
import com.mexico.inloancash.http.IndiaDKFactory;
import com.mexico.inloancash.utils.IndiaAdjust_Helper;
import com.mexico.inloancash.utils.MyPreferencesManager;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author zx
 * @description
 * @date 2020/1/15.
 */
public class IndiaLoginModelImpl implements IndiaLoginContract.LoginModel {
    @Override
    public void login(String username, String password,final IndiaCallBack callBack) {
        IndiaRequestApi api = IndiaDKFactory.getInstance().getApi();
        Call<String> login = api.login(username,password, MyApplication.source);
        login.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                try {
                    if(response.isSuccessful()){
                        India_User_Login_Bean bean = new Gson().fromJson(response.body(), India_User_Login_Bean.class);
                        if(bean.getCode() == 200){
                            //这里登录判断 如果是首次登录 就视为注册 就行adjust统计
                            if (bean.getData().getLogin() == 0) {
                                //0 首次登录 就是注册 进行统计
                                IndiaAdjust_Helper.firstRegister();
                            }
                            MyPreferencesManager.putString("token",bean.getData().getToken());
                            MyPreferencesManager.putLogin(true);
                            callBack.success("Login successful");
                        }else {
                            callBack.failed("Login failed");
                        }
                    }
                }catch (Exception e){

                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                callBack.failed("Login failed");
            }
        });
    }
}
